<?php

/*
 * 日程/任务
 */
class Schedule_model extends CI_Model {
	var $conf;
	public function __construct() {
		parent :: __construct();
		$this->conf = array (
			'start_day' => 'sunday',
			'month_type' => 'long',
			'show_next_prev' => true,
		'next_prev_url' => base_url() . 'index.php/schedule/display');
		$this->conf['template'] = '
										{table_open}<table border="0" cellpadding="0" cellspacing="0" class="calendar">{/table_open}

					   {heading_row_start}<tr class="rc_tit">{/heading_row_start}

					   {heading_previous_cell}<th colspan="7"><a href="{previous_url}">&lt;&lt;</a>{/heading_previous_cell}
					    {heading_title_cell}<span>{heading}</span>{/heading_title_cell}
					    {heading_next_cell}<a href="{next_url}">&gt;&gt;</a></th>{/heading_next_cell}

					    {heading_row_end}</tr>{/heading_row_end}

					   {week_row_start}<tr class="g_tit_cont">{/week_row_start}
					    {week_day_cell}<td>{week_day}</td>{/week_day_cell}
					    {week_row_end}</tr>{/week_row_end}

					    {cal_row_start}<tr class="days">{/cal_row_start}
					    {cal_cell_start}<td>{/cal_cell_start}

					   {cal_cell_content}
							<h2 class="day_num">{day}<a href="index.php/schedule/add/{year}-{month}-{day}" class="various1" title="新建行动历史"><img alt="" src="images/12act.png"></a></h2>
							<h2 class="content">{content}</h2><!----非当天内容--->
						{/cal_cell_content}
						{cal_cell_content_today}
							<h2 class="day_num highlight">{day}<a href="index.php/schedule/add/{year}-{month}-{day}" class="various1" title="新建行动历史"><img alt="" src="images/12act.png"></a></h2><!----当天标题--->
							<h2 class="content">{content}</h2><!----当天内容--->
						{/cal_cell_content_today}

						{cal_cell_no_content}<h2 class="day_num">{day}<a href="index.php/schedule/add/{year}-{month}-{day}" class="various1" title="新建行动历史"><img alt="" src="images/12act.png"></a></h2>{/cal_cell_no_content}
						{cal_cell_no_content_today}<div class="day_num highlight">{day}<a href="index.php/schedule/add/{year}-{month}-{day}" class="various1" title="新建行动历史"><img alt="" src="images/12act.png"></a></div>{/cal_cell_no_content_today}

					    {cal_cell_blank}&nbsp;{/cal_cell_blank}

					   {cal_cell_end}</td>{/cal_cell_end}
					    {cal_row_end}</tr>{/cal_row_end}

					   {table_close}</table>{/table_close}

					        		';
	}

	function get_calendar_data($year, $month) {
		$user_id = $this->session->userdata('id');
		$role = $this->session->userdata('role');
		if (empty ($year) && empty ($month)) {
			date_default_timezone_set("PRC");
			$emptytime = date("Y-m");
			$year = date('Y');
			$month = date('m');
		} else {
			$emptytime = $year . "-" . $month;
		}
		if($role == '市场'){
			$query = $this->db->select('schedule_id,execution_time,subject,importance,status')->from('schedule')->where('user_id',$user_id)->like('execution_time', $emptytime, 'after')->get();
		}else{
			$query = $this->db->select('schedule_id,execution_time,subject,importance,status,user.*')->from('schedule')->join('user','schedule.user_id=user.id')->like('execution_time', $emptytime, 'after')->get();
			$division="----";
		}

		$val = '';
		$today = date('j');

		foreach ($query->result() as $rows) {
			$day = date('j', strtotime($rows->execution_time));
			if (!empty ($day)) {
				if($rows->importance == '非常重要'){
						$class_name='content_bg1';
					}
					if($rows->importance == '重要'){
						$class_name='content_bg2';
					}
					if($rows->importance == '普通'){
						$class_name='content_bg';
					}
					$status = '';
					if($rows->status == '取消'){
						$status = '<b style="background:url(images/01.png) left center no-repeat; display:block; height:10px; width:10px; float:left; margin-top:4px; padding-right:3px;"> </b>';
					}
					if($rows->status == '完成'){
						$status = '<b style="background:url(images/02.png) left center no-repeat; display:block; height:10px; width:10px; float:left; margin-top:5px; padding-right:3px;"> </b>';
					}
					if($rows->status == '执行中'){
						$status = '<b style="background:url(images/27.png) left center no-repeat; display:block; height:10px; width:10px; float:left; margin-top:5px; padding-right:3px;"> </b>';
					}
					if($rows->status == '未开始'){
						$status = '<b style="background:url(images/50.png) left center no-repeat; display:block; height:10px; width:10px; float:left; margin-top:5px; padding-right:3px;"> </b>';
					}
				if ($today == $day) {
					$cal_data[$day] .= '<a href="index.php/schedule/check/' . $year . '-' . $month . '-' . $day . '/' . $rows->schedule_id . '" class="'.$class_name.' various1"><span>'.$status.'' . $this->utf_substr($rows->subject,10)."&nbsp;$division $rows->user_name". '</span></a>';
				} else {
					$cal_data[$day] .= '<a href="index.php/schedule/check/' . $year . '-' . $month . '-' . $day . '/' . $rows->schedule_id . '" class="'.$class_name.' various1"><span>'.$status.'' . $this->utf_substr($rows->subject,10)."&nbsp;$division $rows->user_name" . '</span></a>';
				}
			}
		}
		return $cal_data;
	}

	function generate($year, $month) {
		$this->load->library('calendar', $this->conf);
		$cal_data = $this->get_calendar_data($year, $month);

		return $this->calendar->generate($year, $month, $cal_data);
	}

	function utf_substr($str,$len){
		for($i=0;$i<$len;$i++){
				$temp_str=substr($str,0,1);
				if(ord($temp_str) > 127){
					$i++;
					if($i<$len){
						$new_str[]=substr($str,0,3);
						$str=substr($str,3);
					}
				}else{
					$new_str[]=substr($str,0,1);
					$str=substr($str,1);
				}
		}
	return join($new_str);
}

}
?>